package com.example.sdb;

import android.app.Activity;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.webkit.ValueCallback;
import android.webkit.WebResourceRequest;
import android.webkit.WebResourceResponse;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;

import androidx.core.content.ContextCompat;

import org.json.JSONObject;

public class WebViewActivity extends Activity {
    private WebView webView;

    private GestureDetector gestureDetector;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (Build.VERSION.SDK_INT >= 21){
            View decorView = getWindow().getDecorView();

//            decorView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
//            decorView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
//            getWindow().setStatusBarColor(Color.TRANSPARENT);
//            getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_HIDE_NAVIGATION);

//            // 隐藏电池栏
//            getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
//                    WindowManager.LayoutParams.FLAG_FULLSCREEN);
//
//            // 隐藏导航栏
//            getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_HIDE_NAVIGATION);

//            getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
//
//                    WindowManager.LayoutParams.FLAG_FULLSCREEN);

            //透明状态栏
//            getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);

//透明导航栏

//            getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);


        }
        setContentView(R.layout.activity_web_view);
        webView = findViewById(R.id.webView);

        // 在需要访问用户信息的时刻
//        String userInfoJson = webView.evaluateJavascript(
//                "(function() { return localStorage.getItem('userInfo'); })();",
//                value -> {
//                    // 处理返回的用户信息JSON字符串
//                    JSONObject userInfo = new JSONObject(value);
//                    // 使用用户信息
//                }
//        );
//        webView.evaluateJavascript("(function() { return localStorage.getItem('phone3'); })();", new ValueCallback<String>() {
//            @Override
//            public void onReceiveValue(String s) {
//                //将button显示的文字改成JS返回的字符串
////                buttonLeft.setText(s);
//                System.out.println("sssssssssssssssss"+s);
//            }
//        });


        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            Window window = getWindow();
            window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
            window.setStatusBarColor(ContextCompat.getColor(this, R.color.your_status_bar_color));
        }if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            Window window = getWindow();
            window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
            window.setStatusBarColor(ContextCompat.getColor(this, R.color.your_status_bar_color));
        }
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
//            Window window = getWindow();
//            window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
//            window.setStatusBarColor(ContextCompat.getColor(this, R.color.your_status_bar_color));
                    View decore= getWindow().getDecorView();
                    decore.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
        }

        // 允许WebView打开新的窗口(例如：点击链接跳转)
        webView.setWebViewClient(new WebViewClient());


        // 启用缓存
        webView.getSettings().setCacheMode(WebSettings.LOAD_DEFAULT);

        // 设置可以存储数据
                webView.getSettings().setDomStorageEnabled(true);

//        // 开启Application Caches 功能
//                webView.getSettings().setAppCacheEnabled(true);
//
//        // 设置Application Caches 的存储目录
//                webView.getSettings().setAppCachePath(getApplicationContext().getCacheDir().getAbsolutePath());

        // 允许JavaScript
                webView.getSettings().setJavaScriptEnabled(true);

        // 其他配置...

        // 设置WebView客户端
        webView.setWebViewClient(new WebViewClient());


        // 在WebView初始化时设置允许跨域请求
        webView.getSettings().setAllowUniversalAccessFromFileURLs(true);
        webView.getSettings().setAllowFileAccessFromFileURLs(true);
        webView.getSettings().setJavaScriptEnabled(true);

        webView.setWebViewClient(new WebViewClient() {
            @Override
            public WebResourceResponse shouldInterceptRequest(WebView view, WebResourceRequest request) {
                // 允许所有跨域请求
                return super.shouldInterceptRequest(view, request);
            }
        });

        // 加载assets目录下的index.html
//        webView.loadUrl("file:///android_asset/dist/index.html");
        webView.loadUrl("file:///android_asset/dist/build/h5/index.html");



        gestureDetector = new GestureDetector(this, new GestureDetector.SimpleOnGestureListener() {
            @Override
            public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
                // 检测用户是否从左到右滑动
                if (e1.getX() - e2.getX() > 100 && e1.getY() - e2.getY() < 100) {
                    // 用户从左到右滑动，执行返回操作
//                    onBackPressed();
                    System.out.println("xxxxxxxxxxxxxxxxxxxxxxxxxxxx");
                    return true;
                }
                return false;
            }
        });
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        return gestureDetector.onTouchEvent(event);
    }

    @Override
    public void onBackPressed() {
        System.out.println("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa");
//        finish();
//        System.exit(0);
        // 用户点击返回键时，退出应用
//        moveTaskToBack(true);
        // 可选择finish(); 来关闭当前Activity
        // 或者 super.onBackPressed(); 如果你想要标准的后退操作
    }

    // 其他方法...
}